package com.controller
{
	import com.events.CompanyMasterEvent;
	import com.model.valueObjects.VoCompanyMaster;
	import com.service.CompanyMaster;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.managers.CursorManager;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;

	public class CompanyMasterController extends AbstractController
	{
		import com.model.Model;
		
		[Inject]
		public var companyMasterService:CompanyMaster;
		
		
		//[PostConstruct]
		public function CompanyMasterController()
		{
			super();	
		}
		
		[EventHandler(event="CompanyMasterEvent.GET_ALL_COMPANY")]
		public function getAllCompany():void{
			sh.executeServiceCall(companyMasterService.getAllCompanies(),getAllCompaniesResult, handleFault );
		}
		private function getAllCompaniesResult(event:ResultEvent):void
		{
			model.companyMasterList=event.result as ArrayCollection;
			CursorManager.removeBusyCursor();
			model.busyCurrsorOn=true;
		}
		
		[EventHandler(event="CompanyMasterEvent.GET_COMPANY")]
		public function getCompany():void{
			sh.executeServiceCall(companyMasterService.getCompany(),getCompaniesResult, handleFault );
		}
		private function getCompaniesResult(event:ResultEvent):void
		{
			model.companyList=event.result as ArrayCollection;
		}
		
		[EventHandler(event="CompanyMasterEvent.ADD_COMPANY", properties="companyMastervo")]
		public function addCompany(companyMastervo:VoCompanyMaster):void
		{
			sh.executeServiceCall(companyMasterService.addNewCompany(companyMastervo), addCompanyResult, handleFault );	
		}
		private function addCompanyResult(event:ResultEvent):void
		{
			model.companyMasterList=event.result as ArrayCollection;
			CursorManager.removeBusyCursor();
			model.busyCurrsorOn=true;
			Alert.show("New Company Added" );
		}
		
		[EventHandler(event="CompanyMasterEvent.UPDATE_COMPANY", properties="companyMastervo")]
		public function updateCompany(companyMastervo:VoCompanyMaster):void
		{
			sh.executeServiceCall(companyMasterService.editCompany(companyMastervo), updateCompanyResult, handleFault );	
		}
		private function updateCompanyResult(event:ResultEvent):void
		{
			model.companyMasterList=event.result as ArrayCollection;
			CursorManager.removeBusyCursor();
			model.busyCurrsorOn=true;
			Alert.show("Details Updated ");
		}
		
		[EventHandler(event="CompanyMasterEvent.DELETE_COMPANY", properties="serialnumber")]
		public function deleteCompany(serialnumber:Number):void
		{
			sh.executeServiceCall(companyMasterService.deleteCompany(serialnumber), deleteCompanyResult, handleFault );	
		}
		private function deleteCompanyResult(event:ResultEvent):void
		{
			getAllCompany();
			CursorManager.removeBusyCursor();
			model.busyCurrsorOn=true;
			Alert.show("Record Deleted " );
		}
	
		private function handleFault(event:FaultEvent):void
		{
			Alert.show( event.fault.faultDetail, "Error" );
		}
	}
}